<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Attributes of Compound Components</title>
<link rel="stylesheet" href="../../../../../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../../index.html" title="Spirit X3 3.10">
<link rel="up" href="../abstracts.html" title="Abstracts">
<link rel="prev" href="primitive_attributes.html" title="Attributes of Primitive Components">
<link rel="next" href="more_compound_attributes.html" title="More About Attributes of Compound Components">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../boost.png"></td>
<td align="center"><a href="../../../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="primitive_attributes.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../abstracts.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="more_compound_attributes.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="spirit_x3.abstracts.compound_attributes"></a><a class="link" href="compound_attributes.html" title="Attributes of Compound Components">Attributes of
      Compound Components</a>
</h3></div></div></div>
<p>
        <span class="emphasis"><em>Spirit.X3</em></span> implement well defined attribute type propagation
        rules for all compound parsers, such as sequences, alternatives, Kleene star,
        etc. The main attribute propagation rule for a sequences is for instance:
      </p>
<pre class="programlisting"><span class="identifier">a</span><span class="special">:</span> <span class="identifier">A</span><span class="special">,</span> <span class="identifier">b</span><span class="special">:</span> <span class="identifier">B</span> <span class="special">--&gt;</span> <span class="special">(</span><span class="identifier">a</span> <span class="special">&gt;&gt;</span> <span class="identifier">b</span><span class="special">):</span> <span class="identifier">tuple</span><span class="special">&lt;</span><span class="identifier">A</span><span class="special">,</span> <span class="identifier">B</span><span class="special">&gt;</span>
</pre>
<p>
        which reads as:
      </p>
<div class="blockquote"><blockquote class="blockquote"><p>
          Given <code class="computeroutput"><span class="identifier">a</span></code> and <code class="computeroutput"><span class="identifier">b</span></code> are parsers, and <code class="computeroutput"><span class="identifier">A</span></code>
          is the attribute type of <code class="computeroutput"><span class="identifier">a</span></code>,
          and <code class="computeroutput"><span class="identifier">B</span></code> is the attribute
          type of <code class="computeroutput"><span class="identifier">b</span></code>, then the attribute
          type of <code class="computeroutput"><span class="identifier">a</span> <span class="special">&gt;&gt;</span>
          <span class="identifier">b</span></code> (<code class="computeroutput"><span class="identifier">a</span>
          <span class="special">&lt;&lt;</span> <span class="identifier">b</span></code>)
          will be <code class="computeroutput"><span class="identifier">tuple</span><span class="special">&lt;</span><span class="identifier">A</span><span class="special">,</span> <span class="identifier">B</span><span class="special">&gt;</span></code>.
        </p></blockquote></div>
<div class="note"><table border="0" summary="Note">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../images/note.png"></td>
<th align="left">Note</th>
</tr>
<tr><td align="left" valign="top"><p>
          The notation <code class="computeroutput"><span class="identifier">tuple</span><span class="special">&lt;</span><span class="identifier">A</span><span class="special">,</span> <span class="identifier">B</span><span class="special">&gt;</span></code> is used as a placeholder expression
          for any fusion sequence holding the types A and B, such as <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">fusion</span><span class="special">::</span><span class="identifier">tuple</span><span class="special">&lt;</span><span class="identifier">A</span><span class="special">,</span> <span class="identifier">B</span><span class="special">&gt;</span></code> or <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">pair</span><span class="special">&lt;</span><span class="identifier">A</span><span class="special">,</span> <span class="identifier">B</span><span class="special">&gt;</span></code>
          (for more information see <a href="../../../../../../../libs/fusion/doc/html/index.html" target="_top">Boost.Fusion</a>).
        </p></td></tr>
</table></div>
<p>
        As you can see, in order for a type to be compatible with the attribute type
        of a compound expression it has to
      </p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
            either be convertible to the attribute type,
          </li>
<li class="listitem">
            or it has to expose certain functionalities, i.e. it needs to conform
            to a concept compatible with the component.
          </li>
</ul></div>
<p>
        Each compound component implements its own set of attribute propagation rules.
        For a full list of how the different compound parsers consume attributes
        see the sections Parser Compound Attribute Rules.
      </p>
<h5>
<a name="spirit_x3.abstracts.compound_attributes.h0"></a>
        <span class="phrase"><a name="spirit_x3.abstracts.compound_attributes.the_attribute_of_sequence_parsers"></a></span><a class="link" href="compound_attributes.html#spirit_x3.abstracts.compound_attributes.the_attribute_of_sequence_parsers">The
        Attribute of Sequence Parsers</a>
      </h5>
<p>
        Sequences require an attribute type to expose the concept of a fusion sequence,
        where all elements of that fusion sequence have to be compatible with the
        corresponding element of the component sequence. For example, the expression:
      </p>
<pre class="programlisting"><span class="identifier">double_</span> <span class="special">&gt;&gt;</span> <span class="identifier">double_</span>
</pre>
<p>
        is compatible with any fusion sequence holding two types, where both types
        have to be compatible with <code class="computeroutput"><span class="keyword">double</span></code>.
        The first element of the fusion sequence has to be compatible with the attribute
        of the first <code class="computeroutput"><span class="identifier">double_</span></code>, and
        the second element of the fusion sequence has to be compatible with the attribute
        of the second <code class="computeroutput"><span class="identifier">double_</span></code>. If
        we assume to have an instance of a <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">pair</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">,</span> <span class="keyword">double</span><span class="special">&gt;</span></code>,
        we can directly use the expressions above to do both, parse input to fill
        the attribute:
      </p>
<pre class="programlisting"><span class="comment">// the following parses "1.0 2.0" into a pair of double</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">input</span><span class="special">(</span><span class="string">"1.0 2.0"</span><span class="special">);</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">::</span><span class="identifier">iterator</span> <span class="identifier">strbegin</span> <span class="special">=</span> <span class="identifier">input</span><span class="special">.</span><span class="identifier">begin</span><span class="special">();</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">pair</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">,</span> <span class="keyword">double</span><span class="special">&gt;</span> <span class="identifier">p</span><span class="special">;</span>
<span class="identifier">x3</span><span class="special">::</span><span class="identifier">phrase_parse</span><span class="special">(</span><span class="identifier">strbegin</span><span class="special">,</span> <span class="identifier">input</span><span class="special">.</span><span class="identifier">end</span><span class="special">(),</span>
    <span class="identifier">x3</span><span class="special">::</span><span class="identifier">double_</span> <span class="special">&gt;&gt;</span> <span class="identifier">x3</span><span class="special">::</span><span class="identifier">double_</span><span class="special">,</span>       <span class="comment">// parser grammar</span>
    <span class="identifier">x3</span><span class="special">::</span><span class="identifier">space</span><span class="special">,</span>                        <span class="comment">// delimiter grammar</span>
    <span class="identifier">p</span><span class="special">);</span>                               <span class="comment">// attribute to fill while parsing</span>
</pre>
<div class="tip"><table border="0" summary="Tip">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Tip]" src="../../images/tip.png"></td>
<th align="left">Tip</th>
</tr>
<tr><td align="left" valign="top"><p>
          <span class="bold"><strong>For sequences only:</strong></span> To keep it simple,
          unlike __Spirit.qi__, <span class="emphasis"><em>Spirit.X3</em></span> does not support more
          than one attribute anymore in the <code class="computeroutput"><span class="identifier">parse</span></code>
          and <code class="computeroutput"><span class="identifier">phrase_parse</span></code> function.
          Just use <code class="computeroutput"><span class="identifier">std</span><span class="special">:</span><span class="identifier">tuple</span><span class="error">'</span><span class="special">.</span> <span class="identifier">Be</span> <span class="identifier">sure</span> <span class="identifier">to</span>
          <span class="identifier">include</span> </code>boost/fusion/adapted/std_tuple.hpp'
          in this case.
        </p></td></tr>
</table></div>
<h5>
<a name="spirit_x3.abstracts.compound_attributes.h1"></a>
        <span class="phrase"><a name="spirit_x3.abstracts.compound_attributes.the_attribute_of_alternative_parsers"></a></span><a class="link" href="compound_attributes.html#spirit_x3.abstracts.compound_attributes.the_attribute_of_alternative_parsers">The
        Attribute of Alternative Parsers</a>
      </h5>
<p>
        Alternative parsers are all about - well - alternatives. In order to store
        possibly different result (attribute) types from the different alternatives
        we use the data type <a href="http://www.boost.org/doc/html/variant.html" target="_top">Boost.Variant</a>.
        The main attribute propagation rule of these components is:
      </p>
<pre class="programlisting"><span class="identifier">a</span><span class="special">:</span> <span class="identifier">A</span><span class="special">,</span> <span class="identifier">b</span><span class="special">:</span> <span class="identifier">B</span> <span class="special">--&gt;</span> <span class="special">(</span><span class="identifier">a</span> <span class="special">|</span> <span class="identifier">b</span><span class="special">):</span> <span class="identifier">variant</span><span class="special">&lt;</span><span class="identifier">A</span><span class="special">,</span> <span class="identifier">B</span><span class="special">&gt;</span>
</pre>
<p>
        Alternatives have a second very important attribute propagation rule:
      </p>
<pre class="programlisting"><span class="identifier">a</span><span class="special">:</span> <span class="identifier">A</span><span class="special">,</span> <span class="identifier">b</span><span class="special">:</span> <span class="identifier">A</span> <span class="special">--&gt;</span> <span class="special">(</span><span class="identifier">a</span> <span class="special">|</span> <span class="identifier">b</span><span class="special">):</span> <span class="identifier">A</span>
</pre>
<p>
        often simplifying things significantly. If all sub expressions of an alternative
        expose the same attribute type, the overall alternative will expose exactly
        the same attribute type as well.
      </p>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2001-2018 Joel de Guzman,
      Hartmut Kaiser<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="primitive_attributes.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../abstracts.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="more_compound_attributes.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
